package cn.achao.csr.system.controller;

import cn.achao.csr.api.model.dto.RegisterDTO;
import cn.achao.csr.api.model.dto.AuthDTO;
import cn.achao.csr.api.model.entity.LoginUser;
import cn.achao.csr.base.constant.SecurityConstants;
import cn.achao.csr.base.constant.enums.ThirdAuthEnum;
import cn.achao.csr.base.resp.R;
import cn.achao.csr.base.utils.StringUtils;
import cn.achao.csr.security.annotation.InnerAuth;
import cn.achao.csr.api.model.entity.SysUser;
import cn.achao.csr.system.core.model.build.LoginUserBuild;
import cn.achao.csr.system.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: achao
 **/
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class SysUserController {

    private final SysUserService userService;

    /**
     * 注册
     * @param req
     * @return
     */
    @PostMapping("/register")
    public R<LoginUser> register(@RequestBody @Validated RegisterDTO req) {
        SysUser register = userService.register(req);
        LoginUser sysUserVo = new LoginUser();
        sysUserVo.setUser(register);
        userService.getUserRoles(sysUserVo);
        userService.getUserPermissions(sysUserVo);
        return R.ok(sysUserVo);
    }

    /**
     * 获取当前用户信息
     */
    @InnerAuth
    @GetMapping("/info/{userAccount}")
    public R<LoginUser> info(@PathVariable("userAccount") String userAccount) {
        SysUser sysUser = userService.selectUserByUserAccount(userAccount);
        if (StringUtils.isNull(sysUser)) {
            return R.fail("用户名或密码错误");
        }

        LoginUser sysUserVo = LoginUserBuild.buildBySysUser(sysUser);
        userService.getUserRoles(sysUserVo);
        userService.getUserPermissions(sysUserVo);
        return R.ok(sysUserVo);
    }

    /**
     * 根据openId查询用户信息
     * @param openId
     * @return
     */
    @InnerAuth
    @GetMapping("/infoByZfbOpenId/{openId}")
    R<LoginUser> infoByZfbOpenId(@PathVariable("openId") String openId) {
        SysUser sysUser = userService.infoByZfbOpenId(openId);

        LoginUser sysUserVo = LoginUserBuild.buildBySysUser(sysUser);
        userService.getUserRoles(sysUserVo);
        userService.getUserPermissions(sysUserVo);
        return R.ok(sysUserVo);
    }

    /**
     * 授权
     * @param req
     * @return
     */
    @InnerAuth
    @PostMapping("/updateOpenId")
    R<LoginUser> updateZfbOpenId(@RequestBody @Validated AuthDTO req) {
        SysUser sysUser = userService.updateOpenId(req.getUserAccount(), req.getOpenId(), req.getUserInfo(), req.getThirdAuth());

        LoginUser sysUserVo = LoginUserBuild.buildBySysUser(sysUser);
        userService.getUserRoles(sysUserVo);
        userService.getUserPermissions(sysUserVo);
        return R.ok(sysUserVo);
    }

    /**
     * 第三方账号是否存在
     * @param openId
     * @param thirdAuthEnum
     * @return
     */
    @InnerAuth
    @GetMapping("/isExistOpenId")
    R<Boolean> isExistOpenId(@RequestParam("openId") String openId,
                             @RequestParam("thirdAuthEnum") ThirdAuthEnum thirdAuthEnum){
        return R.ok(userService.isExistOpenId(openId, thirdAuthEnum));
    }


    /**
     * 查询用户列表
     * @param userIds
     * @return
     */
    @InnerAuth
    @GetMapping("/getUserListByIds")
    R<Map<Long,SysUser>> getUserListByIds(@RequestParam("openId") List<Long> userIds){
        return R.ok(userService.getBatchByIds(userIds));
    }
}
